home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / os / sprite / WaitFor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  7.6 KB  |  294 lines

  1. /***********************************************************
  2. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /*****************************************************************
  26.  * OS Depedent input routines:
  27.  *
  28.  *  WaitForSomething,  GetEvent
  29.  *
  30.  *****************************************************************/
  31.  
  32. #include "Xos.h"            /* for strings, fcntl, time */
  33.  
  34. #include <errno.h>
  35. #include <stdio.h>
  36. #include "X.h"
  37. #include "misc.h"
  38.  
  39. #include <sys/param.h>
  40. #include <signal.h>
  41. #include "osdep.h"
  42. #include "dixstruct.h"
  43. #include "opaque.h"
  44.  
  45. extern long AllSockets[];
  46. extern long AllClients[];
  47. extern long LastSelectMask[];
  48. extern long WellKnownConnections;
  49. extern long EnabledDevices[];
  50. extern long ClientsWithInput[];
  51. extern long ClientsWriteBlocked[];
  52. extern long OutputPending[];
  53.  
  54. extern long ScreenSaverTime;               /* milliseconds */
  55. extern long ScreenSaverInterval;               /* milliseconds */
  56. extern int ConnectionTranslation[];
  57.  
  58. extern Bool NewOutputPending;
  59. extern Bool AnyClientsWriteBlocked;
  60.  
  61. extern void CheckConnections();
  62. extern void EstablishNewConnections();
  63. extern void SaveScreens();
  64.  
  65. extern int errno;
  66.  
  67. #ifdef apollo
  68. extern long apInputMask[];
  69.  
  70. static long LastWriteMask[mskcnt];
  71. #endif
  72.  
  73. #ifdef XTESTEXT1
  74. /*
  75.  * defined in xtestext1dd.c
  76.  */
  77. extern int playback_on;
  78. #endif /* XTESTEXT1 */
  79.  
  80. /*****************
  81.  * WaitForSomething:
  82.  *     Make the server suspend until there is
  83.  *    1. data from clients or
  84.  *    2. input events available or
  85.  *    3. ddx notices something of interest (graphics
  86.  *       queue ready, etc.) or
  87.  *    4. clients that have buffered replies/events are ready
  88.  *
  89.  *     If the time between INPUT events is
  90.  *     greater than ScreenSaverTime, the display is turned off (or
  91.  *     saved, depending on the hardware).  So, WaitForSomething()
  92.  *     has to handle this also (that's why the select() has a timeout.
  93.  *     For more info on ClientsWithInput, see ReadRequestFromClient().
  94.  *     pClientsReady is an array to store ready client->index values into.
  95.  *****************/
  96.  
  97. static long timeTilFrob = 0;        /* while screen saving */
  98.  
  99. #if (mskcnt>4)
  100. /*
  101.  * This is a macro if mskcnt <= 4
  102.  */
  103. ANYSET(src)
  104.     long    *src;
  105. {
  106.     int i;
  107.  
  108.     for (i=0; i<mskcnt; i++)
  109.     if (src[ i ])
  110.         return (TRUE);
  111.     return (FALSE);
  112. }
  113. #endif
  114. extern void sunInputAvail();
  115.  
  116. int
  117. WaitForSomething(pClientsReady)
  118.     int *pClientsReady;
  119. {
  120.     int i;
  121.     struct timeval waittime, *wt;
  122.     long timeout;
  123.     long clientsReadable[mskcnt];
  124.     long clientsWritable[mskcnt];
  125.     long curclient;
  126.     int selecterr;
  127.     int nready;
  128.     long devicesReadable[mskcnt];
  129.  
  130.     CLEARBITS(clientsReadable);
  131.  
  132.     /* We need a while loop here to handle 
  133.        crashed connections and the screen saver timeout */
  134.     while (1)
  135.     {
  136.     if (ANYSET(ClientsWithInput))
  137.     {
  138.         COPYBITS(ClientsWithInput, clientsReadable);
  139.         break;
  140.     }
  141.     if (ScreenSaverTime)
  142.     {
  143.         timeout = ScreenSaverTime - TimeSinceLastInputEvent();
  144.         if (timeout <= 0) /* may be forced by AutoResetServer() */
  145.         {
  146.         long timeSinceSave;
  147.  
  148.         timeSinceSave = -timeout;
  149.         if ((timeSinceSave >= timeTilFrob) && (timeTilFrob >= 0))
  150.         {
  151.             SaveScreens(SCREEN_SAVER_ON, ScreenSaverActive);
  152.             if (ScreenSaverInterval)
  153.             /* round up to the next ScreenSaverInterval */
  154.             timeTilFrob = ScreenSaverInterval *
  155.                 ((timeSinceSave + ScreenSaverInterval) /
  156.                     ScreenSaverInterval);
  157.             else
  158.             timeTilFrob = -1;
  159.         }
  160.         timeout = timeTilFrob - timeSinceSave;
  161.         }
  162.         else
  163.         {
  164.         if (timeout > ScreenSaverTime)
  165.             timeout = ScreenSaverTime;
  166.         timeTilFrob = 0;
  167.         }
  168.         if (timeTilFrob >= 0)
  169.         {
  170.         waittime.tv_sec = timeout / MILLI_PER_SECOND;
  171.         waittime.tv_usec = (timeout % MILLI_PER_SECOND) *
  172.                     (1000000 / MILLI_PER_SECOND);
  173.         wt = &waittime;
  174.         }
  175.         else
  176.         {
  177.         wt = NULL;
  178.         }
  179.     }
  180.     else
  181.         wt = NULL;
  182.     COPYBITS(AllSockets, LastSelectMask);
  183. #ifdef apollo
  184.         COPYBITS(apInputMask, LastWriteMask);
  185. #endif
  186.     BlockHandler((pointer)&wt, (pointer)LastSelectMask);
  187.     if (NewOutputPending)
  188.         FlushAllOutput();
  189. #ifdef XTESTEXT1
  190.     /* XXX how does this interact with new write block handling? */
  191.     if (playback_on) {
  192.         wt = &waittime;
  193.         XTestComputeWaitTime (&waittime);
  194.     }
  195. #endif /* XTESTEXT1 */
  196.     /* keep this check close to select() call to minimize race */
  197.     if (dispatchException)
  198.         i = -1;
  199.     else if (AnyClientsWriteBlocked)
  200.     {
  201.         COPYBITS(ClientsWriteBlocked, clientsWritable);
  202.         i = select (MAXSOCKS, (int *)LastSelectMask,
  203.             (int *)clientsWritable, (int *) NULL, wt);
  204.     }
  205.     else
  206. #ifdef apollo
  207.         i = select (MAXSOCKS, (int *)LastSelectMask,
  208.             (int *)LastWriteMask, (int *) NULL, wt);
  209. #else
  210.         i = select (MAXSOCKS, (int *)LastSelectMask,
  211.             (int *) NULL, (int *) NULL, wt);
  212. #endif
  213.     selecterr = errno;
  214.     WakeupHandler((unsigned long)i, (pointer)LastSelectMask);
  215. #ifdef XTESTEXT1
  216.     if (playback_on) {
  217.         i = XTestProcessInputAction (i, &waittime);
  218.     }
  219. #endif /* XTESTEXT1 */
  220.     if (i <= 0) /* An error or timeout occurred */
  221.     {
  222.         if (dispatchException)
  223.         return 0;
  224.         CLEARBITS(clientsWritable);
  225.         if (i < 0) 
  226.         if (selecterr == EBADF || selecterr == EIO)    /* Some client disconnected */
  227.         {
  228.             perror("selecterr");
  229.             CheckConnections ();
  230.             if (! ANYSET (AllClients))
  231.             fprintf(stderr, "WaitForSomething returning 0\n");
  232.             return 0;
  233.         }
  234.         else if (selecterr != EINTR)
  235.             ErrorF("WaitForSomething(): select: errno=%d\n",
  236.             selecterr);
  237.             if (selecterr==EIO) {
  238.             /*
  239.              * I don't think we can get out of this alive.
  240.              */
  241.             exit(-1);
  242.             }
  243.     }
  244.     else
  245.     {
  246.         if (AnyClientsWriteBlocked && ANYSET (clientsWritable))
  247.         {
  248.         NewOutputPending = TRUE;
  249.         ORBITS(OutputPending, clientsWritable, OutputPending);
  250.         UNSETBITS(ClientsWriteBlocked, clientsWritable);
  251.         if (! ANYSET(ClientsWriteBlocked))
  252.             AnyClientsWriteBlocked = FALSE;
  253.         }
  254.  
  255.         MASKANDSETBITS(devicesReadable, LastSelectMask, EnabledDevices);
  256. #ifdef    hpux
  257.             /* call the HIL driver to gather inputs.     */
  258.         if (ANYSET(devicesReadable)) store_inputs (devicesReadable);
  259. #endif /* hpux */
  260.  
  261.         MASKANDSETBITS(clientsReadable, LastSelectMask, AllClients); 
  262.         if (LastSelectMask[0] & WellKnownConnections) 
  263.         EstablishNewConnections();
  264.         if (ANYSET (devicesReadable) || ANYSET (clientsReadable))
  265.         break;
  266.     }
  267.     }
  268.  
  269.     nready = 0;
  270.     if (ANYSET(clientsReadable))
  271.     {
  272.     for (i=0; i<mskcnt; i++)
  273.     {
  274.         while (clientsReadable[i])
  275.         {
  276.         curclient = ffs (clientsReadable[i]) - 1;
  277.         pClientsReady[nready++] = 
  278.             ConnectionTranslation[curclient + (i << 5)];
  279.         clientsReadable[i] &= ~(1 << curclient);
  280.         }
  281.     }    
  282.     }
  283. #if defined(sun4) || defined(sun3)
  284. /* Added by TvE (hack) : */
  285.     if(ANYSET(devicesReadable))
  286.         sunInputAvail();
  287. /* End TvE hack */
  288. #endif
  289.     return nready;
  290. }
  291.  
  292.  
  293.  
  294.